const Monitor = require('../../utils/monitor.js').Monitor
var util = require('../../utils/util.js')
Page(Monitor.hookApp({
  data: {
    status: 'all',
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    top: `${getApp().globalData.imgUrl}jsy-newshop上复制 2.svg`,
    bottom: `${getApp().globalData.imgUrl}jsy-newshop下箭头复制 2.svg`,
    ketixianjin: false,
    chooseIsShow: false, //隐藏的菜单展示条件
    currentTab: 0,
    hiddenCurrentTab: 0,
    arr: [1, 2, 3],
    arr2: [1, 2],
    hiddenList: ['全部', '储值', '完善奖励', '商户消费', '签到', '退款'],
    dataArr: [],
    clickIndex: '' //点击的索引
  },

  click(e) { //点击上面导航
    let ev = e.currentTarget.dataset.current
    if (this.data.currentTab == ev) { } else {
      this.setData({
        currentTab: ev,
      })
    }
  },
  clickHidden(e) {
    var e2 = e.currentTarget.dataset.hidden, //点击隐藏菜单子元素索引
      arr = this.data.dataArr
    arr.forEach((item) => {
      item[0].typeShow = false
    })
    if (this.data.hiddenCurrentTab == e) { } else {
      this.setData({
        hiddenCurrentTab: e2, //给类名的 
        chooseIsShow: !this.data.chooseIsShow, //让整个菜单隐藏
        status: 'all'
      })
    }
  },
  showHidden(e) { //隐藏的账单的展示与隐藏
    var arr = this.data.dataArr,
      e1 = e.currentTarget.dataset.current // 当前点击的索引
    if (arr[e1][0].isShow == false) { // 还没点开（关闭 --》 展示）
      if (arr[e1][1].length == 0) {
        wx.showToast({
          title: '此月没有交易信息',
          icon: 'none'
        })
        return
      }
      arr.forEach((ele) => {
        ele[0].isShow = false // 控制下拉菜单
        ele[0].typeShow = false // 控制类型菜单
      })
    }
    arr[e1][0].isShow = !arr[e1][0].isShow
    arr[e1][0].typeShow = false
    this.setData({
      dataArr: arr,
      chooseIsShow: false,
      hiddenCurrentTab: 0,
      clickIndex: e1,
      key: arr[e1][0].date
    })
    console.log(arr)
  },
  choiceKind(e) { //点右边交易类型
    var arr = this.data.dataArr
    var e1 = e.currentTarget.dataset.current
    if (arr[e1][0].isShow == true) { //如果他已经被点开
      arr.forEach((item, index) => { //关闭所有右侧的箭头
        if (index == e1) {
          item[0].typeShow = !item[0].typeShow
        } else {
          item[0].typeShow = false
        }
      })
      this.setData({
        chooseIsShow: arr[e1][0].typeShow,
        dataArr: arr
      })
    }
  },
  getInfo() {
    util.API('spwx/v1.Member/myYue', {
      status: this.data.status
    }).then(({
      code,
      msg,
      data
    }) => {
      if (code == 200) {
        let orders = data.order,
          dataArr = [],
          {
            allmoney,
            cashmoney,
            order,
            storage
          } = data;
        for (let prop in orders) {
          let arr = [{},
          []
          ]
          arr[0].date = prop
          arr[1] = orders[prop]
          arr[0].isShow = false
          arr[0].typeShow = false
          dataArr.push(arr)
        }
        this.setData({
          allmoney,
          cashmoney,
          order,
          storage,
          dataArr
        })
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })
  },
  choiceType(e) {
    let type = e.currentTarget.dataset.type,
      that = this;
    if (type == 0) {
      this.setData({
        status: 'all',
        canIClick: true
      })
    } else if (type == 1) {
      this.setData({
        status: 'storage',
        canIClick: true
      })
    } else if (type == 2) {
      this.setData({
        status: 'supply',
        canIClick: true
      })
    } else if (type == 3) {
      this.setData({
        status: 'normal',
        canIClick: true
      })
    } else if (type == 4) {
      this.setData({
        status: 'sign',
        canIClick: true
      })
    } else if (type == 5) {
      this.setData({
        status: 'refund',
        canIClick: true
      })
    }
    if (this.data.canIClick) {
      util.API('spwx/v1.Member/myYue', {
        status: that.data.status
      }).then(({
        code,
        data,
        msg
      }) => {
        if (code == 200) {
          let {
            dataArr,
            clickIndex,
            key
          } = this.data
          if (data.order[key].length != 0) {
            dataArr[clickIndex][1] = data.order[key]
            dataArr.forEach(ele => {
              ele[0].typeShow = false
            })
          } else {
            dataArr.forEach(ele => {
              ele[0].typeShow = false
              ele[0].isShow = false
            })
            wx.showToast({
              title: '没有更多数据了',
              icon: 'none'
            })
          }
          this.setData({
            dataArr
          })
          that.clickHidden(e)
        } else {
          wx.showToast({
            title: msg + '',
            icon: 'none'
          })
        }
      })
    }
  },
  onLoad(options) {

  },
  onShow() {
    this.getInfo()
  }
}))